package com.tinusgraglin.enclusiv.ui

import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.HorizontalPager
import com.google.accompanist.pager.rememberPagerState
import com.google.accompanist.pager.pagerTabIndicatorOffset
import com.tinusgraglin.enclusiv.R
import com.tinusgraglin.enclusiv.datalayer.ShowedArticleProfile
import com.tinusgraglin.enclusiv.ui.components.ArticleCardsList
import com.tinusgraglin.enclusiv.ui.theme.*
import com.tinusgraglin.enclusiv.ui.viewmodel.MainActivityViewModel
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch

@OptIn(ExperimentalPagerApi::class)
@Composable
fun UniverseScreen(viewModel: MainActivityViewModel, onRecommendedItemClicked: (Int, ShowedArticleProfile?) -> Unit) {
    val pagerState = rememberPagerState()
    val scope = rememberCoroutineScope()

    val tabItems = listOf(
        R.string.ui_universe_screen_tab_waves,
        R.string.ui_universe_screen_tab_listened
    )

    Surface (
        modifier = Modifier.fillMaxSize(),
    ) {
        Column {
            TabRow (
                modifier = Modifier
                    .wrapContentHeight(),
                selectedTabIndex = pagerState.currentPage,
                divider = {},
                indicator = { tabPositions ->
                    TabRowDefaults.Indicator(
                        modifier = Modifier.pagerTabIndicatorOffset(pagerState, tabPositions)
                    )
                },
                backgroundColor = if (isSystemInDarkTheme()) DimDark else Color.White,
                contentColor = PurpleMain
            ) {
                tabItems.forEachIndexed { idx, textRes ->
                    Tab(
                        selected = pagerState.currentPage == idx,
                        onClick = {
                            scope.launch {
                                pagerState.animateScrollToPage(idx)
                            }
                        },
                        selectedContentColor = PurpleMain,
                        unselectedContentColor = if (isSystemInDarkTheme()) DarkModeGrey else UnselecetedGrey
                    ) {
                        Text(modifier = Modifier.padding(vertical = 10.dp), text = stringResource(textRes), fontSize = 16.sp, fontWeight = if (pagerState.currentPage == idx) FontWeight.Bold else FontWeight.Normal)
                    }
                }
            }
            HorizontalPager (
                count = tabItems.size,
                state = pagerState,
                modifier = Modifier.fillMaxSize()
            ) { page ->
                when (page) {
                    0 -> RecommendScreen(viewModel, onRecommendedItemClicked)
                    1 -> SubscribeScreen(viewModel)
                }
            }
        }
    }
}

@Composable
fun RecommendScreen(viewModel: MainActivityViewModel, onItemClicked: (Int, ShowedArticleProfile?) -> Unit) {
    val scope = rememberCoroutineScope()
    val list by viewModel.recommended.collectAsState()
    val isRefreshing by produceState(initialValue = false, key1 = null) {
        viewModel.refreshingStates.map { it.recommendIsRefreshing }.distinctUntilChanged().collect {
            this.value = it
        }
    }

    ArticleCardsList(
        showedArticleProfileList = list,
        isRefreshing = isRefreshing,
        onRefresh = { viewModel.getOneShotRecommended() }
    ) { i, it ->
        onItemClicked(i, it)
    }
}

@Composable
fun SubscribeScreen(viewModel: MainActivityViewModel) {
    Column(modifier = Modifier.fillMaxSize()) {
        Text("s2text1")
        Text("s2text2")
    }
}